﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.ZhaoLuService;
using org.in2bits.MyXls;

namespace CommunitySystem
{
    public partial class TiJianControl_ZLGL : UserControl
    {
        private ZhaoLuService.ZhaoLuServiceClient Client1 = new ZhaoLuServiceClient();
        private ObservableCollection<ZhaoLuJiHuaModel> poi =
           new ObservableCollection<ZhaoLuJiHuaModel>();
        private ObservableCollection<ZhaoLu_DaoRuKaoShengModel> selectedempmodel =
            new ObservableCollection<ZhaoLu_DaoRuKaoShengModel>();

        public string TJJG { get; set; }
        public TiJianControl_ZLGL()
        {
            InitializeComponent();
            //for (int i = DateTime.Today.Year; i > DateTime.Today.Year - 10; i--)
            //{
            //    ComboBoxItem cbi = new ComboBoxItem();
            //    cbi.Content = i.ToString();
            //    cbi.Tag = i;
            //    nian.Items.Add(cbi);
            //}
            //nian.SelectedIndex = 0;
            Client1.GetZhaoLu_DaoRuKaoShengModelCompleted += new EventHandler<GetZhaoLu_DaoRuKaoShengModelCompletedEventArgs>(Client1_GetZhaoLu_DaoRuKaoShengModelCompleted);
            Client1.QiQuanCompleted += new EventHandler<QiQuanCompletedEventArgs>(Client1_QiQuanCompleted);
            Client1.UpdateZL_TiJianCompleted += new EventHandler<UpdateZL_TiJianCompletedEventArgs>(Client1_UpdateZL_TiJianCompleted);
            
            Client1.GetZhaoLuJiHuaModelForComCompleted += new EventHandler<GetZhaoLuJiHuaModelForComCompletedEventArgs>(Client1_GetZhaoLuJiHuaModelForComCompleted);
            Client1.GetZhaoLuJiHuaModelForComAsync();
            zucha.DataContext = this;

          
        }

      
        void Client1_GetZhaoLuJiHuaModelForComCompleted(object sender, GetZhaoLuJiHuaModelForComCompletedEventArgs e)
        {
            poi = e.Result;
            ObservableCollection<ZhaoLuJiHuaModel> poi1 = new ObservableCollection<ZhaoLuJiHuaModel>();
            poi1.Add(poi.Where(p => p.IsLocked == false).FirstOrDefault());
            nian.ItemsSource = poi1;
            if (poi1.Count > 0) nian.SelectedIndex = 0;
            Load();
        }
        private int tiaojian = 0;
        void gangwei_SelectionChanged1(object sender, SelectionChangedEventArgs e)
        {
            if (((ComboBox)sender).SelectedItem != null)
            {
                tiaojian = ((ZhaoLuJiHuaModel)(((ComboBox)sender).SelectedItem)).ID;
            }
        }
       

        public void Load()
        {
            Client1.GetZhaoLu_DaoRuKaoShengModelAsync(tiaojian, false, 0, 0, "拟同意", TJJG, "", null, "", "无", "0");
        }
        private ObservableCollection<ZhaoLu_DaoRuKaoShengModel> temp =
            new ObservableCollection<ZhaoLu_DaoRuKaoShengModel>();
        void Client1_GetZhaoLu_DaoRuKaoShengModelCompleted(object sender, GetZhaoLu_DaoRuKaoShengModelCompletedEventArgs e)
        {
         
            if (e.Result != null && e.Result.Count > 0)
            {
                for (int i = 0; i < e.Result.Count; i++)
                {
                    e.Result[i].Sec = i + 1;

                }
            }
            DGRole.ItemsSource = null;
            DGRole.ItemsSource = e.Result;
        }
        /// <summary>
        /// 保存体检结果
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Data_Click(object sender, RoutedEventArgs e)
        {
            selectedempmodel.Clear();
            count = 0;
            if (DGRole.ItemsSource != null)
            {
                foreach (ZhaoLu_DaoRuKaoShengModel i in (ObservableCollection<ZhaoLu_DaoRuKaoShengModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                foreach (ZhaoLu_DaoRuKaoShengModel model in selectedempmodel)
                {
                    if(model.TiJianResult == "暂缓")
                    {
                        if(string.IsNullOrEmpty(model.TiJianBeiZhu))
                        {
                            SilverlightMessageBoxLibrary.Message.ErrorMessage("体检结果为暂缓，请填写备注！");
                            return;
                        }
                    }
                }
                foreach (ZhaoLu_DaoRuKaoShengModel model in selectedempmodel)
                {
                    Client1.UpdateZL_TiJianAsync(model);
                    count = count + 1;
                }
            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要保存的体检结果！"); 
        }
        private int count = 0;
        void Client1_UpdateZL_TiJianCompleted(object sender, UpdateZL_TiJianCompletedEventArgs e)
        {
            //if (count == selectedempmodel.Count)
            //{
            //    count = count - 1;
                //SilverlightMessageBoxLibrary.Message.InfoMessage("保存成功!");
                Load();
            //}
        }
        /// <summary>
        /// 弃权
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fangqi_Click(object sender, RoutedEventArgs e)
        {
            selectedempmodel.Clear();
            if (DGRole.ItemsSource != null)
            {
                foreach (ZhaoLu_DaoRuKaoShengModel i in (ObservableCollection<ZhaoLu_DaoRuKaoShengModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                if (MessageBox.Show("确定进行弃权操作吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    string ss = "";
                    string ss1 = "";
                    foreach (ZhaoLu_DaoRuKaoShengModel i in selectedempmodel)
                    {
                        if(i.TiJianResult == "合格") ss += i.ID.ToString() + ",";
                        if (i.TiJianResult == "暂缓") ss1 += i.ID.ToString() + ",";
                    }
                 
                    Client1.QiQuanAsync(ss.TrimEnd(','), "政审前");
                  
                    Client1.QiQuanAsync(ss1.TrimEnd(','), "体检前");
                }

            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要弃权的人员！"); 
        }

        private bool can = false;
        void Client1_QiQuanCompleted(object sender, QiQuanCompletedEventArgs e)
        {
            if (can)
            {
                MessageBox.Show("弃权成功！");
                Load();
                can = false;
            }
            else can = true;
        }
        private void ChaData_Click(object sender, RoutedEventArgs e)
        {
            Load();
        }
        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            foreach (ZhaoLu_DaoRuKaoShengModel p in DGRole.ItemsSource)
            {
                p.IsSelected = (bool)cb.IsChecked;
                if (p.IsSelected)
                {
                    if (selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(p);
                    }
                }
                else
                {
                    if (selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault() != null)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault());
                    }
                }
            }
        }
        private Stream ss = null;
        /// <summary>
        /// 导出  XlsDocument
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SaveData_Click(object sender, RoutedEventArgs e)
        {
            selectedempmodel.Clear();
            if (DGRole.ItemsSource != null)
            {
                foreach (ZhaoLu_DaoRuKaoShengModel i in (ObservableCollection<ZhaoLu_DaoRuKaoShengModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                var downLoad = new SaveFileDialog()
                {
                    //Filter = "Excel files(2007~2010) (*.xlsx)|*.xlsx|Excel files(2003) (*.xls)|*.xls", //"Excel files (*.xlsx)|*.xlsx|(*.xls)|*.xls",
                    Filter = "Excel files(2003) (*.xls)|*.xls",
                };
                if (downLoad.ShowDialog() == true)
                {
                    ss = downLoad.OpenFile();
                    PageInit();
                }
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("没有数据可以导出！");
                return;
            }
        }
        public void PageInit()
        {
            XlsDocument xls = new XlsDocument();
            XF xf = xls.NewXF();
            xf.HorizontalAlignment = HorizontalAlignments.Centered;
            xf.VerticalAlignment = VerticalAlignments.Centered;
            xf.UseBorder = true;
            xf.TopLineStyle = 1;
            xf.TopLineColor = org.in2bits.MyXls.Colors.Black;
            xf.BottomLineStyle = 1;
            xf.BottomLineColor = org.in2bits.MyXls.Colors.Black;
            xf.LeftLineStyle = 1;
            xf.LeftLineColor = org.in2bits.MyXls.Colors.Black;
            xf.RightLineStyle = 1;
            xf.RightLineColor = org.in2bits.MyXls.Colors.Black;
            xf.TextWrapRight = true;

            XF xf1 = xls.NewXF();
            xf1.UseBorder = true;
            xf1.TopLineStyle = 1;
            xf1.TopLineColor = org.in2bits.MyXls.Colors.Black;
            xf1.BottomLineStyle = 1;
            xf1.BottomLineColor = org.in2bits.MyXls.Colors.Black;
            xf1.LeftLineStyle = 1;
            xf1.LeftLineColor = org.in2bits.MyXls.Colors.Black;
            xf1.RightLineStyle = 1;
            xf1.RightLineColor = org.in2bits.MyXls.Colors.Black;
            xf1.HorizontalAlignment = HorizontalAlignments.Centered;
            xf1.VerticalAlignment = VerticalAlignments.Centered;
            xf1.Font.Bold = true;
            xf1.Font.Height = 15 * 20;

            XF xf3 = xls.NewXF();
            xf3.UseBorder = true;
            xf3.TopLineStyle = 1;
            xf3.TopLineColor = org.in2bits.MyXls.Colors.Black;
            xf3.BottomLineStyle = 1;
            xf3.BottomLineColor = org.in2bits.MyXls.Colors.Black;
            xf3.LeftLineStyle = 1;
            xf3.LeftLineColor = org.in2bits.MyXls.Colors.Black;
            xf3.RightLineStyle = 1;
            xf3.RightLineColor = org.in2bits.MyXls.Colors.Black;
            xf3.HorizontalAlignment = HorizontalAlignments.Centered;
            xf3.VerticalAlignment = VerticalAlignments.Centered;
            xf3.Font.Bold = true;
            xf3.Font.Height = 12 * 20;

            Worksheet sheet1 = xls.Workbook.Worksheets.AddNamed("人员体检表");
            sheet1.Cells.Merge(1, 1, 1, 7);
            sheet1.Cells.Add(1, 1, "徐汇区" + (DateTime.Today.Year - nian.SelectedIndex).ToString() + "年招录人员体检表", xf1);
            sheet1.Cells.Add(2, 1, "序号", xf3);
            //sheet1.Cells.Add(2, 2, "名次", xf3);
            sheet1.Cells.Add(2, 2, "姓名", xf3);
            sheet1.Cells.Add(2, 3, "身份证号", xf3);
            sheet1.Cells.Add(2, 4, "报名单位", xf3);
            sheet1.Cells.Add(2, 5, "报名职务", xf3);
            sheet1.Cells.Add(2, 6, "体检结果", xf3);
            //sheet1.Cells.Add(2, 7, "笔试分数", xf3);
            //sheet1.Cells.Add(2, 8, "面试分数", xf3);
            //sheet1.Cells.Add(2, 9, "总分数", xf3);

            ColumnInfo colInfoxueli1 = new ColumnInfo(xls, sheet1);
            colInfoxueli1.Width = 15 * 256;
            colInfoxueli1.ColumnIndexStart =2;
            colInfoxueli1.ColumnIndexEnd = 2;
            sheet1.AddColumnInfo(colInfoxueli1);

            ColumnInfo colInfoxueli2 = new ColumnInfo(xls, sheet1);
            colInfoxueli2.Width = 25 * 256;
            colInfoxueli2.ColumnIndexStart = 3;
            colInfoxueli2.ColumnIndexEnd = 3;
            sheet1.AddColumnInfo(colInfoxueli2);

            ColumnInfo colInfoxueli3 = new ColumnInfo(xls, sheet1);
            colInfoxueli3.Width = 20 * 256;
            colInfoxueli3.ColumnIndexStart = 4;
            colInfoxueli3.ColumnIndexEnd = 4;
            sheet1.AddColumnInfo(colInfoxueli3);

            for (int i = 0; i < selectedempmodel.Count; i++)
            {
                sheet1.Cells.Add(i + 3, 1, i + 1, xf);
                //sheet1.Cells.Add(i + 3, 2, selectedempmodel[i].MingCi.ToString(), xf);
                if (selectedempmodel[i].XingMing != null) sheet1.Cells.Add(i + 3, 2, selectedempmodel[i].XingMing.ToString(), xf);
                if (selectedempmodel[i].SFZH != null) sheet1.Cells.Add(i + 3, 3, selectedempmodel[i].SFZH.ToString(), xf);
                if (selectedempmodel[i].YRBM != null) sheet1.Cells.Add(i + 3, 4, selectedempmodel[i].YRBM.ToString(), xf);
                if (selectedempmodel[i].BBZW != null) sheet1.Cells.Add(i + 3, 5, selectedempmodel[i].BBZW.ToString(), xf);
                if (selectedempmodel[i].TiJianResult != null) sheet1.Cells.Add(i + 3, 6, selectedempmodel[i].TiJianResult.ToString(), xf);
            }
            xls.Save(ss);
            ss.Close();
            ss.Dispose();
            SilverlightMessageBoxLibrary.Message.InfoMessage("导出成功！");
        }
        private string islock = "0";
        private void chkHiddenCols_Unchecked(object sender, RoutedEventArgs e)
        {
            islock = "0";
            ObservableCollection<ZhaoLuJiHuaModel> poi1 = new ObservableCollection<ZhaoLuJiHuaModel>();
            poi1.Add(poi.Where(p => p.IsLocked == false).FirstOrDefault());
            nian.ItemsSource = poi1;
            if (poi1.Count > 0) nian.SelectedIndex = 0;
            Load();
        }
        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            islock = "1";
            ObservableCollection<ZhaoLuJiHuaModel> poi1 = new ObservableCollection<ZhaoLuJiHuaModel>();
            foreach (ZhaoLuJiHuaModel i in poi)
            {
                if (i.IsLocked) poi1.Add(i);
            }
            nian.ItemsSource = poi1;
            if (poi1.Count > 0) nian.SelectedIndex = 0;
            Load();
        }
        /// <summary>
        /// 体检结果
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbLevel_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var obj = (sender as ComboBox).Tag as ZhaoLu_DaoRuKaoShengModel;
            if (obj != null)
            {
                if (((sender as ComboBox).SelectedValue as ComboBoxItem).Content.ToString() == "暂缓")
                {
                    if (string.IsNullOrEmpty(obj.TiJianBeiZhu))
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("体检结果为暂缓，请填写备注！");

                    }
                }
                obj.TiJianResult = ((sender as ComboBox).SelectedValue as ComboBoxItem).Content.ToString();
                Client1.UpdateZL_TiJianAsync(obj);
            }

        }
        /// <summary>
        /// 备注
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
          
        }
        /// <summary>
        /// 加载列表绑定combox
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void DGRole_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            ComboBox chk = this.DGRole.Columns[6].GetCellContent(e.Row) as ComboBox;
            ZhaoLu_DaoRuKaoShengModel bindData = (ZhaoLu_DaoRuKaoShengModel)e.Row.DataContext;

            if (bindData.TiJianResult == "合格")
                chk.SelectedIndex = 0;
            if (bindData.TiJianResult == "不合格")
                chk.SelectedIndex = 1;
            if (bindData.TiJianResult == "暂缓")
                chk.SelectedIndex = 2;

        }
        private void Text_LostFocus(object sender, EventArgs e)
        {
            var obj = (sender as TextBox).Tag as ZhaoLu_DaoRuKaoShengModel;
            //if (!string.IsNullOrEmpty((sender as TextBox).Text))
            //{
                obj.TiJianBeiZhu = (sender as TextBox).Text;
                Client1.UpdateZL_TiJianAsync(obj);
            //}
        }
    }
}
